/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.server.globalManager.logic;

import java.util.ArrayList;
import java.util.List;
import mscs.data.IFileDataStore;
import mscs.data.IGoodsProxy;
import mscs.data.pojo.Goods;

/**
 *
 * @author Administrator
 */
public class AddGoodsInfoUtil implements IAddGoodsInfoUtil{
    private ArrayList<Goods> newList = new ArrayList<Goods>();
    private IFileDataStore fds = new IGoodsProxy();

    public AddGoodsInfoUtil(){
        
    }
    
    public List<Goods> getNewList() {
        return this.newList;
    }

    public boolean addToNewList(Goods goods) {
        if(this.isValid(goods)){
            this.newList.add(goods);
            return true;
        }else{
            return false;
        }
    }

    

    public boolean setByIndex(int index, Goods goods) {
        if(this.isValid(goods)){
            this.newList.set(index, goods);
            return true;
        }else{
            return false;
        }
    }
    
    private boolean isValid(Goods goods){
        if(!this.fds.searchObjectsByAttr("id", String.valueOf(goods.getId())).isEmpty()){
            return false;
        }
        for(Goods g: newList){
            if(g.getId() == goods.getId())
                return false;
        }
        return true;
    }
    
    public Goods getByIndex(int index) {
        return this.newList.get(index);
    }

    public void removeFromNewList(int index) {
        this.newList.remove(index);
    }

    public boolean commitNewList() {
        if(this.newList.isEmpty()){
            return false;
        }else{
            this.fds.add(newList);
            newList = new ArrayList<Goods>();
            return true;
        }
    }
    
}
